using Lockstep.Collision2D;
using Lockstep.Game;
using Lockstep.Math;
using UnityEngine;
using UnityEngine.UI;
using Debug = Lockstep.Logging.Debug;

namespace Lockstep.Game
{
    public class InputMono : UnityEngine.MonoBehaviour
    {
        private static bool IsReplay => Launcher.Instance?.IsVideoMode ?? false;

        public Button fireBtn, upArrowBtn, rightArrowBtn, downArrowBtn, leftArrowBtn;
        private PlayerInput curInput = new PlayerInput();
        private void Awake()
        {
            fireBtn.gameObject.SetActive(Application.isMobilePlatform);
            upArrowBtn.gameObject.SetActive(Application.isMobilePlatform);
            rightArrowBtn.gameObject.SetActive(Application.isMobilePlatform);
            downArrowBtn.gameObject.SetActive(Application.isMobilePlatform);
            leftArrowBtn.gameObject.SetActive(Application.isMobilePlatform);
            if (Application.isMobilePlatform)
            {
                fireBtn.onClick.AddListener(FireBtnClick);
                upArrowBtn.onClick.AddListener(UpArrowBtnClick);
                rightArrowBtn.onClick.AddListener(RightArrowBtnClick);
                downArrowBtn.onClick.AddListener(DownArowBtnClick);
                leftArrowBtn.onClick.AddListener(LeftArowBtnClick);
            }
            curInput.Reset();
        }

        public void Update()
        {
            if (World.Instance != null && !IsReplay)
            {
                GameInputService.CurGameInput = Application.isMobilePlatform ? CheckOnMobile() : CheckOnPC();
            }
        }

        private void FireBtnClick()
        {
            curInput.isInputFire = true;
        }
        private void UpArrowBtnClick()
        {
            curInput.inputUV.y = 1;
        }
        private void RightArrowBtnClick()
        {
            curInput.inputUV.x = 1;
        }
        private void DownArowBtnClick()
        {
            curInput.inputUV.y = -1;
        }
        private void LeftArowBtnClick()
        {
            curInput.inputUV.x = -1;
        }
        PlayerInput CheckOnMobile()
        {
            PlayerInput input = curInput.Clone();
            curInput.Reset();
            return input;
        }
        PlayerInput CheckOnPC()
        {
            int h = 0, v = 0;
            if (Input.GetKey(KeyCode.A))
            {
                h = -1;
            }
            else if (Input.GetKey(KeyCode.D))
            {
                h = 1;
            }
            if (h == 0)
            {
                if (Input.GetKey(KeyCode.W))
                {
                    v = 1;
                }
                else if (Input.GetKey(KeyCode.S))
                {
                    v = -1;
                }
            }
            
            curInput.inputUV = new LVector2(h.ToLFloat(), v.ToLFloat());
            curInput.isInputFire = Input.GetKey(KeyCode.Space);
            if (!Application.isMobilePlatform)
            {
                //Player2
                int h2 = 0, v2 = 0;
                if (Input.GetKey(KeyCode.LeftArrow))
                {
                    h2 = -1;
                }
                else if (Input.GetKey(KeyCode.RightArrow))
                {
                    h2 = 1;
                }
                if (h2 == 0)
                {
                    if (Input.GetKey(KeyCode.UpArrow))
                    {
                        v2 = 1;
                    }
                    else if (Input.GetKey(KeyCode.DownArrow))
                    {
                        v2 = -1;
                    }
                }
                curInput.inputUV2 = new LVector2(h2.ToLFloat(), v2.ToLFloat());
                curInput.isInputFire2 = Input.GetKey(KeyCode.KeypadEnter);
            }
            PlayerInput input = curInput.Clone();
            curInput.Reset();
            return input;
        }

    }
}